/**
 * @filename:SysUserServiceImpl 2023-10-22 00:33:19
 * @project xxx系统  1.0
 * Copyright(c) 2021 fangdy Co. Ltd.
 * All right reserved.
 */
package org.fdy.bgadmin.service.impl;

import org.fdy.bgadmin.entity.SysUser;
import org.fdy.bgadmin.dao.SysUserDao;
import org.fdy.bgadmin.req.AuthUserReq;
import org.fdy.bgadmin.service.SysUserService;
import org.fdy.common.core.enums.ResultCode;
import org.fdy.common.core.exception.BaseException;
import org.fdy.common.core.model.auth.SaUser;
import org.fdy.common.core.units.LoginUtils;
import org.fdy.common.core.units.RsaUtils;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/**
 * @Description:(sys_user服务实现)
 *
 * @version: 1.0
 * @author: fangdy
 *
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService  {

    @Override
    public Boolean modifyPassword(AuthUserReq req) {
        SaUser loginUser = LoginUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        SysUser one = this.getById(userId);
        if (!RsaUtils.matchesPassword(req.getOldPassword(), one.getPassword())) {
             throw new BaseException(ResultCode.FAIL.getCode(),"密码错误");
        }
        String encryptPassword = RsaUtils.encryptPassword(req.getPass());
        boolean update = this.lambdaUpdate()
                .set(SysUser::getPassword, encryptPassword)
                .eq(SysUser::getId, userId)
                .update();
        return update;
    }
}
